import time
def modifyConf(tfile,sstr,rstr):
    flag = 0
    try:
        lines=open(tfile,'r').readlines()
        open(tfile+".back_up",'w').writelines(lines)
        print("alredy backup conf file,named pg_hba.conf.back_up\n\n\n")
        flen=len(lines)-1
        for i in range(flen):
            if sstr in lines[i]:
                if "127.0.0.1" in lines[i]:
                    if "#" not in lines[i]:
                        lines[i]=lines[i].replace(sstr,rstr)               
                        flag = 1
        open(tfile,'w').writelines(lines)
    except Exception as e:
        flag = -1
    return flag
a = modifyConf('..\..\local\os\windows64\database\pgsql\data\pg_hba.conf','md5','trust')
b = modifyConf('..\..\local\os\windows32\database\pgsql\data\pg_hba.conf','md5','trust')

if(a==1 or b==1):
    print("*****init database successed******")
elif(a==-1 and b==-1):
    print("#something wrong!init database failed#")
else:
    print("database has inited,please don not run this script again")
print("\n\n-------------------------------------------------------------")
print("will close this window after 15s,yoou can close is mutual...")
print("\n\n-------------------------------------------------------------")
time.sleep(15)
